<template>
	<el-dialog v-model="visible" :title="'购买'" :close-on-click-modal="false">
		<el-text class="w-150px mb-2" truncated>确定购买此订单？</el-text>
		<!--		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">-->
		<!--			<el-form-item label="" prop="id">-->
		<!--				<el-input v-model="dataForm.id" placeholder=""></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="订单编号" prop="orderNumber">-->
		<!--				<el-input v-model="dataForm.orderNumber" placeholder="订单编号"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="发货地（国家）" prop="sendCountry">-->
		<!--				<el-input v-model="dataForm.sendCountry" placeholder="发货地（国家）"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="发货地(州)" prop="sendProvince">-->
		<!--				<el-input v-model="dataForm.sendProvince" placeholder="发货地(州)"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="发货地(城市)" prop="sendCity">-->
		<!--				<el-input v-model="dataForm.sendCity" placeholder="发货地(城市)"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="目的地(城市)" prop="goalCountry">-->
		<!--				<el-input v-model="dataForm.goalCountry" placeholder="目的地(城市)"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="目的地(州)" prop="goalProvince">-->
		<!--				<el-input v-model="dataForm.goalProvince" placeholder="目的地(州)"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="目的地(城市)" prop="goalCity">-->
		<!--				<el-input v-model="dataForm.goalCity" placeholder="目的地(城市)"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="发货时间" prop="sendTime">-->
		<!--				<el-input v-model="dataForm.sendTime" placeholder="发货时间"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="备注" prop="remark">-->
		<!--				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="价格" prop="price">-->
		<!--				<el-input v-model="dataForm.price" placeholder="价格"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="类型" prop="catalog">-->
		<!--				<el-input v-model="dataForm.catalog" placeholder="类型"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="单号状态 0-运输途中 1-收到信息 2-成功签收 3-可能异常" prop="orderStatus">-->
		<!--				<el-input v-model="dataForm.orderStatus" placeholder="单号状态 0-运输途中 1-收到信息 2-成功签收 3-可能异常"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="上传时间" prop="createTime">-->
		<!--				<el-input v-model="dataForm.createTime" placeholder="上传时间"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label=" 购买状态 0-未购买 1-已经购买" prop="buyStatus">-->
		<!--				<el-input v-model="dataForm.buyStatus" placeholder=" 购买状态 0-未购买 1-已经购买"></el-input>-->
		<!--			</el-form-item>-->
		<!--			<el-form-item label="购买时间" prop="updateTime">-->
		<!--				<el-input v-model="dataForm.updateTime" placeholder="购买时间"></el-input>-->
		<!--			</el-form-item>-->
		<!--		</el-form>-->
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useMainOrderApi, useMainOrderSubmitApi } from '@/api/order/mainOrder'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	orderNumber: '',
	sendCountry: '',
	sendProvince: '',
	sendCity: '',
	goalCountry: '',
	goalProvince: '',
	goalCity: '',
	sendTime: '',
	remark: '',
	price: '',
	catalog: '',
	orderStatus: '',
	createTime: '',
	buyStatus: '',
	updateTime: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getMainOrder(id)
	}
}

const getMainOrder = (id: number) => {
	useMainOrderApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useMainOrderSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
